package com.bing;

import com.bing.entity.JPAEvent;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Date;
import java.util.List;

/**
 * Created by kaibb on 15-10-31.
 */
public class HelloEnvers {
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @Before
    public void setUp() throws Exception {
        // like discussed with regards to SessionFactory, an EntityManagerFactory is set up once for an application
        // 		IMPORTANT: notice how the name here matches the name we gave the persistence-unit in persistence.xml!
        entityManagerFactory = Persistence.createEntityManagerFactory("jpa");
    }

    @After
    public void tearDown() throws Exception {
        entityManagerFactory.close();
    }


    @Test
    public void testHello() {
        //create entity into DB
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(new JPAEvent("first event", new Date()));
        entityManager.persist(new JPAEvent("second event", new Date()));
        entityManager.getTransaction().commit();
        entityManager.close();

        //select all entities
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        List<JPAEvent> resultList = entityManager.createQuery("from JPAEvent", JPAEvent.class).getResultList();
        for (JPAEvent jpaEvent : resultList) {
            System.out.println(jpaEvent);
        }
        JPAEvent event = resultList.get(1);
        entityManager.getTransaction().commit();
        entityManager.close();

        //select all entities
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        JPAEvent event2 = entityManager.find(JPAEvent.class, event.getId());
        event2.setName("reversion 2");
        entityManager.getTransaction().commit();
        entityManager.close();

        //check edit
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        JPAEvent event3 = entityManager.find(JPAEvent.class, event.getId());
        Assert.assertEquals("reversion 2", event3.getName());
        entityManager.getTransaction().commit();
        entityManager.close();

        entityManager = entityManagerFactory.createEntityManager();
        AuditReader reader = AuditReaderFactory.get(entityManager);
        JPAEvent firstRevision = reader.find(JPAEvent.class, event.getId(), 1);
        Assert.assertFalse(firstRevision.getName().equals(event3.getName()));

        JPAEvent secondRevision = reader.find(JPAEvent.class, event.getId(), 2);
        Assert.assertFalse(secondRevision.getName().equals(event3.getName()));
        entityManager.getTransaction().commit();


    }
}
